<template>
	<div class="table-demo-container layout-padding">
		<div class="table-demo-padding layout-padding-view layout-padding-auto">
			<TableSearch :search="state.tableData.search" @search="onSearch" @addRow="onTableEditRow" />
			<div style="display: flex; overflow: hidden;flex: 1;">
				<div class="menu-left">
					<fast-tree-component :treeData="treeState.treeData" :treeProps="treeProps"
						@selectedCode="selectedCode"></fast-tree-component>
				</div>
				<Table ref="tableRef" v-bind="state.tableData" class="table-demo" @delRow="onTableDelRow"
					@editRow="onTableEditRow" @pageChange="onTablePageChange" @sortHeader="onSortHeader"
					@UpdateCommand="onTableUpdateCommand" />
			</div>
			<editComponent ref="dialogRef" @refresh="getTableData()" />
		</div>
	</div>
</template>

<script setup lang="ts">
import { defineAsyncComponent, reactive, ref, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import { useAccessPointCommandApi, useAccessPointDeleteApi, useAccessPointPageApi } from '/@/api/access/point';
import { EmptyObjectType, RefType } from '/@/types/global';
import { useAccessAreaTreeApi } from '/@/api/access/area';

const dialogRef = ref<RefType>();
const editComponent = defineAsyncComponent(() => import('./editDialog.vue'));
// 引入组件
const Table = defineAsyncComponent(() => import('/@/views/access/point/table.vue'));
const TableSearch = defineAsyncComponent(() => import('/@/components/table/noAddSearch.vue'));
const fastTreeComponent = defineAsyncComponent(() => import('/@/views/access/point/fastTree/index.vue'));

const treeProps = {
	label: 'name',
	key: 'code',
	children: 'children'
}

// 定义变量内容
const tableRef = ref<RefType>();
const state = reactive<TableDemoState>({
	tableData: {
		// 列表数据（必传）
		data: [],
		// 表头内容（必传，注意格式
		header: [
			{ key: 'alias', colWidth: '', title: '门名称', type: 'text', isCheck: true, sortable: true },
			{ key: 'deviceCode', colWidth: '', title: '设备序列号', type: 'text', isCheck: true, sortable: true },
			{ key: 'deviceName', colWidth: '', title: '设备名称', type: 'text', isCheck: true, sortable: true },
			{ key: 'inout', colWidth: '', title: '进出方向', type: 'text', isCheck: true, sortable: true },
			{ key: 'areaName', colWidth: '', title: '设备区域', type: 'text', isCheck: true, sortable: true },
		],
		// 配置项（必传）
		config: {
			total: 0, // 列表总数
			loading: true, // loading 加载
			isBorder: true, // 是否显示表格边框
			isSerialNo: false, // 是否显示表格序号
			isSelection: false, // 是否显示表格多选
			isOperate: true, // 是否显示表格操作栏
		},
		// 搜索表单，动态生成（传空数组时，将不显示搜索，注意格式）
		search: [
			{ label: '门名称', prop: 'alias', placeholder: '请输入门名称', required: false, type: 'input', defaultValue: '' },
			// { label: '设备序列号', prop: 'deviceCode', placeholder: '请输入设备序列号', required: false, type: 'input', defaultValue: '' }
		],
		// 搜索参数（不用传，用于分页、搜索时传给后台的值，`getTableData` 中使用）
		param: {
			parentCode: '',
			pageNum: 1,
			pageSize: 10,
		},
		// 打印标题
		printName: '打印示例',
	},
});
const treeState = reactive({
	treeData: []
})

// 初始化列表数据
const getTableData = () => {
	state.tableData.config.loading = true;
	state.tableData.data = [];
	useAccessPointPageApi(state.tableData.param).then(({ content }) => {
		state.tableData.data = content.list.map((item: any) => {
			return {
				...item,
				inout: item.inout === 1 ? '进' : '出',
			}
		}) || []
		console.log(state.tableData.data)
		state.tableData.param.pageSize = content.pageSize
		state.tableData.param.pageNum = content.pageNum
		state.tableData.config.total = content.total
	})
	// 数据总数（模拟，真实从接口取）
	state.tableData.config.loading = false;
};

// 主页面获取树状菜单
const getTreeMenus = () => {
	useAccessAreaTreeApi(state.tableData.param.parentCode).then(({ content }) => {
		treeState.treeData = content
	})
}
const onTableUpdateCommand = (row: EmptyObjectType, commandCode: string) => {
	console.log(row, commandCode, '12312')
	useAccessPointCommandApi({ pointCode: row, options: commandCode }).then(() => {
		ElMessage.success('执行成功');
	})
}

// 勾选关联查询table
const selectedCode = (data: any) => {
	state.tableData.param = Object.assign({}, state.tableData.param, { areaCodes: data });
	getTableData()
}

// 搜索点击时表单回调
const onSearch = (data: EmptyObjectType) => {
	state.tableData.param = Object.assign({}, state.tableData.param, { ...data });
	// tableRef.value.pageReset();
	getTableData()
};
// 删除当前项回调
const onTableDelRow = (row: EmptyObjectType) => {
	useAccessPointDeleteApi(row.code).then(() => {
		ElMessage.success(`删除${row.name}成功！`);
		getTableData();
	})

};
// 编辑当前项回调
const onTableEditRow = (row: EmptyObjectType) => {
	if (row) {
		dialogRef.value.openDialog(row.code);
	} else {
		dialogRef.value.openDialog();
	}
};
// 分页改变时回调
const onTablePageChange = (page: TableDemoPageType) => {
	state.tableData.param.pageNum = page.pageNum;
	state.tableData.param.pageSize = page.pageSize;
	getTableData();
};
// 拖动显示列排序回调
const onSortHeader = (data: TableHeaderType[]) => {
	state.tableData.header = data;
};
// 页面加载时
onMounted(() => {
	getTableData();
	getTreeMenus()
});
</script>

<style scoped lang="scss">
.table-demo-container {
	.table-demo-padding {
		padding: 15px;

		.table-demo {
			flex: 1;
			overflow: hidden;
		}
	}
}

.menu-left {
	margin-right: 15px;
	overflow-y: auto;
	padding: 0 5px 30px 0;
	min-width: 180px;
}
</style>
